package com.philips.servlet;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import com.alibaba.fastjson.JSON;

public class WeatherServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String result = "";
		String result2 = "";
		String aqi = "";
		Map resmap = new HashMap();
		try {
			resp.setContentType("text/html;charset=UTF-8");
			resp.setCharacterEncoding("UTF-8");
			req.setCharacterEncoding("UTF-8");
			String url = "http://www.weather.com.cn/data/sk/101020100.html";
			String url2 = "http://www.weather.com.cn/data/cityinfo/101020100.html";
			// String urlaqi="http://www.pm25.in/shanghai";
			String urlaqi = "http://ixuanwu.com.cn/book/AQI/citys.php";
			// String url="http://www.weather.com.cn/weather/101020100.shtml";

			HttpClient client = new HttpClient();
			client.getHostConfiguration().setProxy("130.139.104.10", 8080);
			client.getParams().setAuthenticationPreemptive(true);
			client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("310124879", "Philips2"));

			 GetMethod get = new GetMethod(url);
			 get.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//
			// 在头文件中设置转码
			 client.executeMethod(get);
			 result = new String(get.getResponseBodyAsString().getBytes("utf-8"));
			
			 GetMethod get2 = new GetMethod(url2);
			 get2.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//
			// 在头文件中设置转码
			 client.executeMethod(get2);
			 result2 = new  String(get2.getResponseBodyAsString().getBytes("utf-8"));

			GetMethod getaqi = new GetMethod(urlaqi);
			getaqi.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
			client.executeMethod(getaqi);
			aqi = new String(getaqi.getResponseBodyAsString().getBytes("utf-8"));
			// String
			// aqitime=aqi.substring(aqi.indexOf("<div class=\"live_data_time\">"),aqi.indexOf("<!-- end of .live_data_time -->"));
			// aqitime=aqitime.replaceAll("\"", "");
			// aqitime=aqitime.replaceAll("\r", "");
			// aqitime=aqitime.replaceAll("\t", "");
			// aqitime=aqitime.replaceAll("\n", "");
			// aqi=aqi.substring(aqi.indexOf("<table"),aqi.indexOf("</table>")+8);
			// aqi=aqi.replaceAll("\"", "");
			// aqi=aqi.replaceAll("\r", "");
			// aqi=aqi.replaceAll("\t", "");
			// aqi=aqi.replaceAll("\n", "");
			// aqi="{\"aqi\":\""+aqitime+aqi+"\"}";
			aqi=convert(aqi);
			System.out.println("aqi="+aqi);
			aqi=aqi.substring(aqi.indexOf("\"上海\":{\"weather_ex\"")+5,aqi.indexOf("\"定州\":{\"weather_ex")-1);
		//写入文件
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		String res="[" + result + "," + result2 + "," + aqi + "]";
		File f = new File("c:\\temp\\weather.txt");
		BufferedWriter output = new BufferedWriter(new FileWriter(f));
	    output.write(res);
	    output.close();
	    
		resp.getWriter().print(res);
	}

	public static void main(String[] args) {
		
		
	}
	public String convert(String utfString){
		StringBuilder sb=new StringBuilder();
		int i=-1;
		int pos=0;
		while((i=utfString.indexOf("\\u",pos))!=-1){
			sb.append(utfString.substring(pos, i));
			if(i+5<utfString.length()){
				pos=i+6;
				sb.append((char)Integer.parseInt(utfString.substring(i+2,i+6),16));
			}
		}
		return sb.toString();
	}
}
